package com.itheima.Jsoup;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试
 */
public class Test_Jsoup {
    public static void main(String[] args) throws Exception {
        //获取地址
        String url1 = "http://www.itcast.cn";
        //发送请求获取数据
            //创建对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
            //设置请求方式
        HttpPost httpPost = new HttpPost(url1);//注意,这里要记得写上url的字符串类型地址
            //设置参数和请求头
        List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("username","admin"));
        list.add(new BasicNameValuePair("password","123"));
            //改一下适合的编码(后面不写也是可以的)
        HttpEntity httpEntity = new UrlEncodedFormEntity(list,"UTF-8");

            //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
            //获取状态码
        int statusCode = response.getStatusLine().getStatusCode();
        String html = null;
        if (statusCode ==200){
            //获取响应数据
            html = EntityUtils.toString(response.getEntity(), "UTF-8");
//            System.out.println(html);
        }
        //获取document对象
        Document document = Jsoup.parse(html);
        String title = document.title();
//        System.out.println(title);
        /*
            简单的方式:connect地址,然后直接调用获取

         */
        Document document1 = Jsoup.connect(url1).get();
        String title1 = document1.title();
        System.out.println(title1);
        //关闭资源
        httpClient.close();

    }
}
